package com.zm.base.core.util;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.persistence.Id;
import javax.persistence.Table;


public class BaseDaoUtil {
	private static Pattern linePattern=Pattern.compile("_(\\w)");
	private static Pattern humpPattern = Pattern.compile("[A-Z]"); 
	/**
	 * 实体类对象属性转数据库字段
	 * getParam:() 
	 * @author chenhuaming 
	 * @param t
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@SuppressWarnings("rawtypes")
	public  static <T> Map<String,Object> getParam(T t) throws IllegalArgumentException, IllegalAccessException{
		Map<String,Object> params=new HashMap<String,Object>();
		Class clazz=t.getClass();
		Field[] fields=clazz.getDeclaredFields();
		String tableName=BaseDaoUtil.humpToLine(clazz.getSimpleName());
		tableName=tableName.substring(1, tableName.length());
		params.put("table",tableName );//加入表名
		for(int i=0;i<fields.length;i++){//加入不为空的字段
			fields[i].setAccessible(true);
			Object obj=fields[i].get(t);
			if(obj instanceof java.util.Date){
				SimpleDateFormat format=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
				obj=format.format(obj);
			}
			if(obj!=null&&!"".equals(obj)){
				String vauleTemp="\'"+obj.toString()+"\'";
				params.put(BaseDaoUtil.humpToLine(fields[i].getName()),vauleTemp);
			}
		}
		return params;
	}
	/**
	 * 实体类对象属性转数据库字段(不进行驼峰转换)
	 * getParam:() 
	 * @author chenhuaming 
	 * @param t
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@SuppressWarnings("rawtypes")
	public  static <T> Map<String,Object> getParams(T t) throws IllegalArgumentException, IllegalAccessException{
		Map<String,Object> params=new HashMap<String,Object>();
		Class clazz=t.getClass();
		Field[] fields=clazz.getDeclaredFields();
		for(int i=0;i<fields.length;i++){//加入不为空的字段
			fields[i].setAccessible(true);
			Object obj=fields[i].get(t);
			if(obj instanceof java.util.Date){
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				obj=format.format(obj);
			}
			if(obj!=null&&!"".equals(obj)){
				String vauleTemp="\'"+obj.toString()+"\'";
				params.put(fields[i].getName(),vauleTemp);
			}
		}
		return params;
	}
	/**
	 * 驼峰转下滑线
	 * humpToLine:() 
	 * @author chenhuaming 
	 * @param param
	 * @return
	 */
	public static String humpToLine(String param){
		Matcher matcher=humpPattern.matcher(param);
		StringBuffer sb=new StringBuffer();
		while(matcher.find()){
			matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	/**
	 * 下划线转驼峰
	 * lineToHump:() 
	 * @author chenhuaming 
	 * @param param
	 * @return
	 */
	public static String lineToHump(String param){
		param = param.toLowerCase();  
        Matcher matcher = linePattern.matcher(param);  
        StringBuffer sb = new StringBuffer();  
        while(matcher.find()){  
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());  
        }  
        matcher.appendTail(sb);  
        return sb.toString(); 
	}
	/**
	 * 获取注解关联的表名
	 * @param clazz
	 * @return
	 */
	public static <T>  String getTableName(Class<T> clazz){
		Table table= clazz.getAnnotation(Table.class);
		return table.name();
	}
	
	public static<T> String getPK(Class<T> clazz){
		Field[] fields = clazz.getDeclaredFields(); 
		String PK="";
		for (Field field : fields) {
			if(field.isAnnotationPresent(Id.class)){
				PK=field.getName();
				break;
			}
		}
		return PK;
	}
	/**
	 * 为添加设置参数
	 * @param value
	 * @return
	 */
	public static String setParamsInSaveOrUpdate(Object value){
		String values="";
		if (value instanceof byte[] ) {
            values += "'" + new String((byte[]) value) + "'";
        }else if(value instanceof String){
            values += "'" + value + "'";
        }else{
            values += value ;
        }
		return values;
	}
public  static <T> String getClassName(Class<T> clazz){
	return clazz.getSimpleName();
}
}
